<?php

namespace App\Http\Controllers\api;

use App\Helpers\Token;
use App\Http\Controllers\Controller;
use App\Repository\RepositoryInterface\SignInterface;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class SignController extends Controller
{
    protected $sign;

    public function __construct(SignInterface $sign){
        $this->sign = $sign;
    }

    /**
     * 签到页面数据
     * @return mixed|void
     */
    public function checkPage(){
        try {
            $uid = \request()->get('uid');
            return $this->sign->checkPage($uid);
        }catch (\Exception $exception){
            return error($exception->getMessage());
        }
    }

    /**
     * 签到
     * @return array|false|string
     */
    public function sign()
    {
        //开启事物
        DB::beginTransaction();
        try {
            $uid = \request()->get('uid');
            if (!is_numeric($uid) || $uid !=(int)$uid || $uid <= 0){
                return error('uid参数错误',);
            }
            $year = date('Ym');
            $num = request()->get('num');
            $date = date('Ymd');
            if ($date > $year.$num){
                DB::commit();
                return $this->sign->retroactive($uid,$year.$num);
            }elseif ($date < $year.$num){
                return error('时间未到');
            }
            //提交
            DB::commit();
            return $this->sign->Sign($uid);
        }catch (\Exception $exception){
            //回滚
            DB::rollBack();
            return error($exception->getMessage());
        }
    }


}
